package com.bml.architect.springboot.web;

import com.bml.architect.springboot.utils.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mq")
public class RabbitController {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/simple")
    public String simple(@RequestParam(name = "name") String name) {


        System.out.println("添加简单的消息到mq");
        rabbitTemplate.convertAndSend("springboot_queue", "msg is  " + name);
        return "add mssage " + name + " to mq!";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        System.out.println("发布一个天气情况的消息");
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", "四川成都天气晴朗!");

        return "send wather is success！";
    }

    @RequestMapping("/direct")
    public String direct(@RequestParam(name = "key") String key) {
        System.out.println("发布路由模式的消息");

        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, key, "发布一个路由消息，key=" + key);

        return "send direct is success！";
    }

    @RequestMapping("/topic")
    public String topic(@RequestParam(name = "key") String key) {
        System.out.println("发布通配符模式的消息");

        rabbitTemplate.convertAndSend(Constants.TOPICS_EXCHANGE, key, "发布一个通配符消息，key=" + key);

        return "send topic is success！";
    }

    @RequestMapping("/delay")
    public String delay(@RequestParam(name = "key") String key) {
        System.out.println("发布一个延迟的消息");
        String format = String.format("normal.%s", key);
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, format, "发布一个延迟消息，key=" + format);

        return "send topic is success！";
    }

}
